package com.hdoit.apollo.web.shop.apis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.CustomerRecord;
import com.hdoit.apollo.logical.shop.service.ICustomerRecordService;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.viewmodel.shop.CustomerRecordSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/api/v1/customerRecord/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/customerRecord")
public class CustomerRecordApi extends BaseApi{
	
	@Autowired
	private ICustomerRecordService customerRecordService;
	
	@Autowired
	private ICustomerService customerService;
	
	private final String saveCustomerRecord="saveCustomerRecord";
	private final String getCustomerRecordsByScroll="getCustomerRecordsByScroll";
	
    @RequestMapping(value = saveCustomerRecord)
    @ApiOperation(notes = saveCustomerRecord, httpMethod = "POST", value = "保存客户欠款信息")
    public @ResponseBody String saveCustomerRecord(CustomerRecord customerRecord){
    	CustomerRecord customerRecordPan = customerRecordService.queryModel(customerRecord.getRecordId());
    	if(customerRecordPan==null){
    		customerRecord.setCreateDate(DateUtils.getCurrent());
            customerRecord.setCreateUserId(getLoginId());
            customerRecord.setCreateUserName(getLoginName());
            customerRecord.setDeleteMark(YesOrNoInteger.no);
            customerRecord.setModelStatus(ModelStatus.add);
    	}else{
    		customerRecord.setModifyUserId(getLoginId());
    		customerRecord.setModifyUserName(getLoginName());
    		customerRecord.setModifyDate(DateUtils.getCurrent());
    		customerRecord.setModelStatus(ModelStatus.update);
    	}
    	
    	Customer customer=customerService.queryModel(customerRecord.getCustomerId());
    	customer.setCustomerDebt(customerRecord.getReceipt());
    	customer.setModelStatus(ModelStatus.update);
    	customerService.applyChange(customer);
    	
    	ResponseModel rm=new ResponseModel();
    	if(!customerRecordService.applyChange(customerRecord)){
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }

 	@RequestMapping(value = getCustomerRecordsByScroll)
    @ApiOperation(notes = getCustomerRecordsByScroll, httpMethod = "POST", value = "滚动查找获取客户欠款记录列表")
    public @ResponseBody String getStocksByScrollApi(CustomerRecordSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	List<CustomerRecord> customerRecords = customerRecordService.queryListMP(search,pagerModel);
    	rm.put("customerRecords",customerRecords);
    	return rm.toJson();
    }
}
